<template>
	<view class="">
		<view class="bg-container">
			<image src="/static/img/bizhi.jpg"></image>
		</view>
		<view class="container">
			<view class="caozuo">
				<view class="compute">共{{comments.length}}条</view>
				<view @click="doguanli">
					<view class="guanli" v-if="!guanli">管理</view>
					<view class="guanli" v-else>
						完成
					</view>
				</view>
			</view>
			<view>
				<scroll-view class="pu-container" scroll-y="true" @scrolltolower="getMore">
					<view class="pu-item" v-for="(item,index) in comments" :key="index">
						<!-- 复选按钮 -->
						<view class="select" :class="item.select?'selected':''" v-if="guanli" @tap="select(index)">
						</view>
						<!-- 发普信息 -->
						<view :class="guanli?'guanlicomment':'comment'">
							<view class="info">
								<view class="avatar">
									<image :src="item.avatar"></image>
								</view>
								<view class="nickName">{{item.nickName}}</view>
							</view>
							<view class="content">
								<view class="text">{{item.comment}}</view>
								<view class="imgs">
									<view class="photo" v-for="(img,i) in item.imgs" :key="i" @tap="preview(img,i)">
										<image mode="aspectFill" :src="img"></image>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="load" v-if="notHave">
						没有更多数据了
					</view>
				</scroll-view>
			</view>
			<view class="btn" v-if="guanli">
				<view class="btn-quanxuan" @tap="selectAll">
					<view class="quanxuan" :class="select_all?'selected':''"></view>
					<view class="btn-all">{{select_all?'取消全选':'全选'}}</view>
				</view>
				<view class="total">共选中了{{selList.length}}条</view>
				<view class="btn-del" @tap="del">删除</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				comments: [],
				page: 1,
				notHave: false,
				guanli: false,
				selList: [],
				select_all: false
			}
		},
		methods: {
			// 删除评论
			del() {
				let that = this
				// 提示框
				uni.showModal({
					title: '提示',
					content: '确定删除？',
					success: async function(res) {
						if (res.confirm) {
							let delList = that.selList.map(el => {
								return el.id
							})

							let ret = await that.$request({
								url: '/comment/delete',
								data: {
									list: delList.join(',')
								}
							})
							uni.showToast({
								title: '删除成功',
								icon: 'success',
								mask: true
							})
							let timer = setTimeout(() => {
								that.getData()
								clearTimeout(timer)
							}, 1600)

						} else {
							console.log('取消')
						}
					}
				})
			},
			// 点击是否管理
			doguanli() {
				console.log(1)
				this.guanli = !this.guanli
			},
			// 点击选中全部评论
			selectAll() {
				this.select_all = !this.select_all
				this.comments.forEach(el => {
					this.select_all ? (el.select = true) : (el.select = false)
				})
				let newList = this.comments
				this.comments = newList,
					this.selList = newList
				this.watchSel()
			},
			// 封装一个遍历查找selList的函数
			watchSel() {
				let newList = []
				this.selList.forEach(el => {
					el.select && newList.push(el)
				})
				this.selList = newList
			},
			// 点击选择单独的评论
			select(index) {
				let list = this.comments
				//  如果当前为TRUE,则应该改为FALSE
				list[index].select = !list[index].select
				if (list[index].select) {
					this.selList.push(list[index])
				}
				this.comments = list
				this.watchSel()
				this.select_all = this.selList.length == this.comments.length ? true : false
			},
			// 获取所有评论
			getComments(data) {
				return new Promise(async (resolve, reject) => {
					if (!this.notHave) {
						let ret = await this.$request({
							url: '/comment',
							data: data
						})
						console.log(ret)
						let total = ret.total[0].count
						let comments = ret.comments
						comments.forEach(ele => {
							if (ele.photo) {
								ele.imgs = ele.photo.split(',').map(img => this.$serverUrl + '/' + img)
							};
							if(ele.avatar.indexOf('http')==-1){
							ele.avatar = this.$serverUrl + '/' + ele.avatar		
							}
							ele.select = false
						})
						this.comments.push(...comments)
						if (total == this.comments.length) {
							this.notHave = true
						}
					}

				})
			},
			preview(img,i) {
				let current = img
				// console.log(current)
				let urls = this.comments[i].photo.split(',').map(img => this.$serverUrl + '/' + img)
				// console.log(urls)
				uni.previewImage({
					current,
					urls // 需要预览的图片http链接列表
				})
			},
			// 下拉树新更多
			getMore() {
				// 触底增加页码
				this.page + 1
				let openId = JSON.parse(uni.getStorageSync('info'))["openid"]
				this.getComments({
					page: this.page,
					openId
				})
			},
			getData() {
				this.comments = []
				this.page = 1
				this.notHave = false
				this.guanli = false
				this.selList = []
				this.select_all = false
				let openId = JSON.parse(uni.getStorageSync('info'))["openid"]
				this.getComments({
					page: this.page,
					openId
				})
			}
		},
		onLoad: function(options) {
			this.getData()
		},
	}
</script>

<style>
	.bg-container {
		position: fixed;
		top: 0;
		left: 0;
		width: 100vw;
		height: 100vh;
		z-index: -1;
	}

	.container {
		margin-top: 80rpx;
		height: 100vh;
	}

	.bg-container>image {
		width: 100%;
		height: 100%
	}

	.caozuo {
		margin-top: 80rpx;
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		box-sizing: border-box;
		padding: 20rpx 40rpx;
		font-size: 26rpx;
		color: #888;
		background-color: #F6F6F6;
		display: flex;
		justify-content: space-between;
		z-index: 100;
	}

	.pu-container {
		margin-top: 80rpx;
		position: fixed;
		top: 0;
		left: 0;
		height: 100vh;
		width: 100%;
		margin-top: 79rpx;
		box-sizing: border-box;
		padding: 20rpx;
		overflow: hidden;
		padding-bottom: 140rpx;
	}

	.pu-item {
		margin-top: 80rpx;
		margin-bottom: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.select,
	.quanxuan {
		width: 30rpx;
		height: 30rpx;
		border-radius: 50%;
		border: 4rpx solid #F6F6F6
	}

	/* 被选中的样式 */
	.select.selected,
	.quanxuan.selected {
		/* border:0; */
		background-color: #fa3534;
	}

	.comment {
		width: 90%
	}

	.btn {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		display: flex;
		justify-content: space-between;
		background-color: #fff;
		align-items: center;
	}

	.btn-quanxuan {
		width: 35%;
		display: flex;
		align-items: center;
	}

	.total,
	.btn-del,
	.btn-quanxuan {
		box-sizing: border-box;
		padding: 30rpx;
	}

	.total {
		width: 50%;
		text-align: right;
	}

	.btn-del {
		text-align: center;
		width: 20%;
		background-color: #fa3534;
		color: #fff;
	}

	.info {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.avatar {
		width: 10%;
	}

	.avatar image {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
	}

	.nickName {
		width: 85%
	}

	.content {
		background-color: rgba(255, 255, 255, .6);
		box-sizing: border-box;
		padding: 20rpx;
		width: 100%;
		border-radius: 10rpx;
	}

	.guanli {
		display: flex;
	}

	.text {
		box-sizing: border-box;
		padding: 0 20rpx;
		width: 100%;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 3;
		/*设置p元素最大4行，父元素需填写宽度才明显*/
		text-overflow: ellipsis;
		overflow: hidden;
		-webkit-box-orient: vertical;
		word-wrap: break-word;
		word-break: break-all;
	}

	.photo {
		box-sizing: border-box;
		/* padding: 20rpx; marma*/
		margin-left: 20rpx;
		margin-bottom: 20rpx;


	}

	.photo image {
		width: 183rpx;
		height: 240rpx;
	}

	.load {
		color: #666 !important;
		text-align: center;
		box-sizing: border-box;
		padding: 20rpx;
	}

	.imgs {
		display: flex;
		flex-wrap: wrap;
	}

	.comment {
		width: 100%;
	}

	.guanlicomment {
		width: 85%;
	}
</style>
